package demo.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import demo.entity.Product;

public class ProductDaoImpl implements ProductDao{
	
	@Override
	public List<Product> getAllProduct() {
		List<Product> products = new ArrayList<Product>();
		Session session = getSessionFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         products = session.createSQLQuery("select * from product").list(); 
	         
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		return products;
	}
	
	private SessionFactory  getSessionFactory(){
		return new Configuration().configure()
				//.addPackage("demo.entity")
				.addAnnotatedClass(Product.class).buildSessionFactory();
	}

}
